package xyz.yuzegod.megawalls.game;

import org.bukkit.Material;
import org.bukkit.entity.Player;
import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.ItemStack;

import java.util.*;

public class ChestManager
{
    private static List<ChestItem> loot;
    private static List<Integer> slots;
    static Set<Player> givenBow=new HashSet<>();
    
    public static void addItem(final int chance, final Material material, final int durability, final int amount) {
        final ItemStack item = new ItemStack(material);
        item.setAmount(amount);
        if (durability != 0) {
            item.setDurability((short)durability);
        }
        ChestManager.loot.add(new ChestItem(chance, item));
    }
    
    public static void fillInventory(Player triggerer,final Inventory inventory) {
        inventory.clear();
        int added = 0;
        Collections.shuffle(ChestManager.slots);
        final Random random = new Random();
        int i = 0;
        for (final ChestItem chestItem : ChestManager.loot) {
            if (i >= 10) {
                break;
            }
            if (random.nextInt(100) > chestItem.getChance()) {
                continue;
            }
            inventory.setItem((int)ChestManager.slots.get(added), chestItem.getItemStack());
            ++i;
            if (added++ >= 27) {
                break;
            }
        }
        if (!givenBow.contains(triggerer)){
            inventory.addItem(new ItemStack(Material.IRON_AXE));
            givenBow.add(triggerer);
        }
    }
    public static void fillInventory(Player triggerer, final Inventory inventory, List<ItemStack> extra) {
        fillInventory(triggerer,inventory);
        for (ItemStack i:extra){
            inventory.addItem(i);
        }
    }
    
    static {
        ChestManager.loot = new ArrayList<ChestItem>();
        ChestManager.slots = new ArrayList<Integer>();
        for (int i = 0; i < 27; ++i) {
            ChestManager.slots.add(i);
        }
        addItem(45, Material.RAW_FISH, 0, 2);
        addItem(45, Material.APPLE, 0, 3);
        addItem(50, Material.COAL, 0, 3);
        addItem(45, Material.BREAD, 0, 2);
        addItem(70, Material.COOKED_BEEF, 0, 5);
        addItem(45, Material.WHEAT, 0, 3);
        addItem(50, Material.LOG, 0, 1);
        addItem(50, Material.COBBLESTONE, 0, 5);
        addItem(50, Material.FURNACE, 0, 1);
        //addItem(35, Material.IRON_AXE, 0, 1);
        //addItem(35, Material.BOW, 0, 1);
        addItem(35, Material.ARROW, 0, 12);
        addItem(50, Material.IRON_INGOT, 0, 6);
        addItem(25, Material.TNT, 0, 3);
        addItem(50, Material.REDSTONE, 0, 5);
    }
    
    public static class ChestItem
    {
        private int chance;
        private ItemStack itemStack;
        
        public ChestItem(final int chance, final ItemStack itemStack) {
            this.chance = chance;
            this.itemStack = itemStack;
        }
        
        public int getChance() {
            return this.chance;
        }
        
        public ItemStack getItemStack() {
            return this.itemStack;
        }
    }
}
